package com.example.hello;

import com.example.hello.client.ListBox.ListBoxServerRpc;
import com.example.hello.client.ListBox.ListBoxState;
import com.vaadin.event.ConnectorEventListener;
import com.vaadin.event.FieldEvents;
import com.vaadin.event.ShortcutAction;
import com.vaadin.shared.EventId;
import com.vaadin.ui.AbstractComponent;
import com.vaadin.ui.AbstractTextField;
import com.vaadin.ui.Component;
import com.vaadin.util.ReflectTools;

import java.lang.reflect.Method;
import java.util.List;

public class ListBox extends AbstractComponent {

    private ListBoxServerRpc rpc = new ListBoxServerRpc() {
        @Override
        public void sendSelected(String selected) {
            fireEvent(new ListBoxChangeEvent(selected, ListBox.this));
        }
    };

    public ListBox() {
        this(false);
    }

    public ListBox(boolean multipleSelect) {
        getState().setMultipleSelect(multipleSelect);
        registerRpc(rpc);
    }

    public ListBox(String caption) {
        setCaption(caption);
        registerRpc(rpc);
    }

    protected ListBoxState getState() {
        return (ListBoxState) super.getState();
    }

    public List<String> getItems() {
        return getState().getItems();
    }

    public void setItems(List<String> items) {
        getState().setItems(items);
    }

    public void removeAllItems() {
        getState().removeAllItems();
    }

    public void addItem(String item) {
        getState().addItem(item);
    }

    public void addItem(int index, String item) {
        getState().addItem(index, item);
    }

    public boolean removeItem(String item) {
        return getState().removeItem(item);
    }

    public String removeItem(int index) {
        return getState().removeItem(index);
    }

    public List<Boolean> getSelected() {
        return getState().getSelected();
    }

    public void setSelected(List<Boolean> selected) {
        getState().setSelected(selected);
    }

    public boolean getSelected(int index) {
        return getState().getSelected(index);
    }

    public void setItemSelected(int index, boolean selected) {
        getState().setItemSelected(index, selected);
    }

    public void setSelectedValue(String value) {
        if (value == null) {
            value = "";
        }
        for(int i = 0; i < getItems().size(); i++) {
            if (getItems().get(i).equals(value)) {
                setSelectedIndex(i);
                return;
            }
        }
    }

    public boolean isItemSelected(int index) {
        return getState().isItemSelected(index);
    }

    public List<String> getValues() {
        return getState().getValues();
    }

    public void setValues(List<String> values) {
        getState().setValues(values);
    }

    public String getValue(int index) {
        return getState().getValue(index);
    }

    public void setValue(int index, String value) {
        getState().setValue(index, value);
    }

    public void setItemText(int index, String text) {
        getState().setItemText(index, text);
    }

    public boolean isMultipleSelect() {
        return getState().isMultipleSelect();
    }

    public void setMultipleSelect(boolean multipleSelect) {
        getState().setMultipleSelect(multipleSelect);
    }

    public int getSelectedIndex() {
        return getState().getSelectedIndex();
    }

    public void setSelectedIndex(int index) {
        getState().setSelectedIndex(index);
    }

    public int getVisibleItems() {
        return getState().getVisibleItems();
    }

    public void setVisibleItems(int visibleItems) {
        getState().setVisibleItems(visibleItems);
    }

}
